package cn.tedu.eguser1.controller;

import cn.tedu.eguser1.mapper.usermapper;
import cn.tedu.eguser1.pojo.dto.useradd;
import cn.tedu.eguser1.pojo.dto.userupdate;
import cn.tedu.eguser1.pojo.entity.user;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Validated
@Slf4j
@RestController
@RequestMapping("/user")
public class controller
{
    @Autowired
    private usermapper mapper;
    @PostMapping("/add")
    public String add(@RequestBody @Validated useradd add)
    {
        user user= new user();
        BeanUtils.copyProperties(add,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int bb=mapper.insert(user);
        if (bb>0){return "添加用户成功";}
        return "添加用户失败";
    }


    @GetMapping("/select")
    public List<user> select()
    {
        List<user> user=mapper.select();
        for(user u:user){System.out.println(u);}
        return user;

    }

    @PostMapping("/delete")
    public String add(@Range(min = 1, max=10, message = "id须在1-10间") Long id)
    {
        int nn=mapper.delete(id);
        if(nn>0)
        {
            return "删除成功";
        }
        return "删除失败";
    }

    @PostMapping("/update")
    public String update(userupdate up)
    {
        int nn=mapper.update(up);
        if(nn>0)
        {
            return "更新成功";
        }
        return "更新失败";
    }

}
